#from CustomGuiItems.MyDockWidget import MyDockWidget;
import pylab;
from PyQt4 import QtGui, QtCore;

class MazeProbs(QtCore.QObject):    
    def __init__(self, objData):
        super(MazeProbs, self).__init__();
        self.dataModel = objData.dataModel;
        self.objData = objData;
        self.parent = objData.parent;
        self.graphScene = objData.graphScene;
        self.mainWin = objData.parent.mainWin;
        self.objData;
#        self.myDockWidget = MyDockWidget("Maze State Type", self.parent);
#        self.mainWin.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.myDockWidget);
        self.aMazeProbs = [];
        self.createDockContent();
#        self.myDockWidget.setWidget(self.qWidget);
        
    def buildMazeProbsTbl(self):
        aAction1 = [];
        aAction2 = [];
        aAction3 = [];
        aAction4 = [];
        a = round(self.spbxMaxProb.value(),3);
        #Action1
        aAction1.append([a, 0, self.getProb(2), 0, self.getProb(2)]);
        aAction1.append([0, 0, 0.333, 0.333, 0.333]);
        aAction1.append([a, 0, 0, self.getProb(2), self.getProb(2)]);
        aAction1.append([a, 0, self.getProb(3), self.getProb(3), self.getProb(3)]);
        aAction1.append([0, 0, 0.5, 0, 0.5]);
        aAction1.append([0, 0, 0, 0.5, 0.5]);
        aAction1.append([a, 0, 0, self.getProb(2), self.getProb(2)]);
        aAction1.append([a, 0, self.getProb(2), 0, self.getProb(2)]);
        aAction1.append([0, 0, 0, 0, 1]);
        aAction1.append([0, 0, 0, 0.5, 0.5]);
        aAction1.append([a, 0, 0, 0, self.getProb(1)]);
        aAction1.append([0, 0, 0.5, 0, 0.5]);
        aAction1.append([a, 0, 0, 0, self.getProb(1)]);
        aAction1.append([0, 0, 0.333, 0.333, 0.333]);
        aAction1.append([a, 0, self.getProb(3), self.getProb(3), self.getProb(3)]);
        #Action2
        aAction2.append([0, a, self.getProb(2), 0, self.getProb(2)]);
        aAction2.append([0, a, self.getProb(3), self.getProb(3), self.getProb(3)]);
        aAction2.append([0, a, 0, self.getProb(2), self.getProb(2)]);
        aAction2.append([0, 0, 0.333, 0.333, 0.333]);
        aAction2.append([0, a, self.getProb(2), 0, self.getProb(2)]);
        aAction2.append([0, a, 0, self.getProb(2), self.getProb(2)]);
        aAction2.append([0, 0, 0, 0.5, 0.5]);
        aAction2.append([0, 0, 0.5, 0, 0.5]);
        aAction2.append([0, a, 0, 0, self.getProb(1)]);
        aAction2.append([0, 0, 0, 0.5, 0.5]);
        aAction2.append([0, 0, 0, 0, 1]);
        aAction2.append([0, 0, 0.5, 0, 0.5]);
        aAction2.append([0, a, 0, 0, self.getProb(1)]);
        aAction2.append([0, 0, 0.333, 0.333, 0.333]);
        aAction2.append([0, a, self.getProb(3), self.getProb(3), self.getProb(3)]);
        #Action3
        aAction3.append([self.getProb(3), self.getProb(3), a, 0, self.getProb(3)]);
        aAction3.append([0, self.getProb(2), a, 0, self.getProb(2)]);
        aAction3.append([0.333, 0.333, 0, 0, 0.333]);
        aAction3.append([self.getProb(2), 0, a, 0, self.getProb(2)]);
        aAction3.append([0, self.getProb(2), a, 0, self.getProb(2)]);
        aAction3.append([0, 0.5, 0, 0, 0.5]);
        aAction3.append([0.5, 0, 0, 0, 0.5]);
        aAction3.append([self.getProb(2), 0, a, 0, self.getProb(2)]);
        aAction3.append([0, 0.5, 0, 0, 0.5]);
        aAction3.append([0, 0, 0, 0, 1]);
        aAction3.append([0.5, 0, 0, 0, 0.5]);
        aAction3.append([0, 0, a, 0, self.getProb(1)]);
        aAction3.append([0.333, 0.333, 0, 0, 0.333]);
        aAction3.append([0, 0, a, 0, self.getProb(1)]);
        aAction3.append([self.getProb(3), self.getProb(3), a, 0, self.getProb(3)]);
        #Action4
        aAction4.append([0.333, 0.333, 0, 0, 0.333]);
        aAction4.append([0, self.getProb(2), 0, a, self.getProb(2)]);
        aAction4.append([self.getProb(3), self.getProb(3), 0, a, self.getProb(3)]);
        aAction4.append([self.getProb(2), 0, 0, a, self.getProb(2)]);
        aAction4.append([0, 0.5, 0, 0, 0.5]);
        aAction4.append([0, self.getProb(2), 0, a, self.getProb(2)]);
        aAction4.append([self.getProb(2), 0, 0, a, self.getProb(2)]);
        aAction4.append([0.5, 0, 0, 0, 0.5]);
        aAction4.append([0, 0.5, 0, 0, 0.5]);
        aAction4.append([0, 0, 0, a, self.getProb(1)]);
        aAction4.append([0.5, 0, 0, 0, 0.5]);
        aAction4.append([0, 0, 0, 0, 1]);
        aAction4.append([0.333, 0.333, 0, 0, 0.333]);
        aAction4.append([0, 0, 0, a, self.getProb(1)]);
        aAction4.append([self.getProb(3), self.getProb(3), 0, a, self.getProb(3)]);
        self.aMazeProbs.append(aAction1);
        self.aMazeProbs.append(aAction2);
        self.aMazeProbs.append(aAction3);
        self.aMazeProbs.append(aAction4);        
        
    def getProb(self, nV):
        nV = float(nV);
        a = self.spbxMaxProb.value();
        nProb = (1-a)/nV;
        return round(nProb,3);
        
    def printMazeTbl(self):
        self.buildMazeProbsTbl();
        for i in range(len(self.aMazeProbs)):
            for j in range(len(self.aMazeProbs[0])):
                a1 = self.aMazeProbs[i][j][0];
                a2 = self.aMazeProbs[i][j][1];
                a3 = self.aMazeProbs[i][j][2];
                a4 = self.aMazeProbs[i][j][3];
                a5 = self.aMazeProbs[i][j][4];
                print a1, "|", a2, "|", a3, "|", a4, "|", a5;
            print "---------"
        
    def onBtnGenerateClick(self):
        self.dataView = self.objData.dataView;
        self.buildMazeProbsTbl();
        nStates = self.dataModel.nStates;
        nActions = self.dataModel.nActions;
        stInfo = self.dataModel.stInfo;
        stTrans = pylab.zeros((nActions,nStates,nStates));
        aActs = [3,1,0,2]; #Up, Down, Right, Left
        for i in range(nStates):
            stTypeWall = int(stInfo[i][0])-1;
            if stTypeWall>=0 and stTypeWall<15:
                for j in range(nActions):
                    for k in range(5): #Up, Down, Right, Left, Same-Place
                        nProb = self.aMazeProbs[j][stTypeWall][k];
                        iSt = self.getState(i, k);
                        iAct = aActs[j];
                        if iSt >= 0:
                            stTrans[iAct][i][iSt] = nProb;
        self.dataModel.stateTrans = stTrans;
        self.dataView.gridStTrans.updateGrid();
    
    def getState(self, stCurrent, stNextType):
        stInfo = self.dataModel.stInfo;
        nStates = self.dataModel.nStates;        
        nStX, nStY = stInfo[stCurrent][1], stInfo[stCurrent][2];
        nextStX, nextStY = int(nStX), int(nStY);
        if stNextType==0: #Up
            nextStY += 1;
        if stNextType==1: #Down
            nextStY -= 1;
        if stNextType==2: #Right
            nextStX += 1;
        if stNextType==3: #Left
            nextStX -= 1;
        if stNextType==4: #Same-Place
            return stCurrent;
        for i in range(nStates):
            stX, stY = int(stInfo[i][1]), int(stInfo[i][2]);
            if nextStX==stX and nextStY==stY:
                return i;
        return -1;
        
    def createDockContent(self):
        #Max Prob Input
        self.lblMaxProb = QtGui.QLabel("Action Max Prob:", self.mainWin);
        self.spbxMaxProb = QtGui.QDoubleSpinBox(self.mainWin);
        self.spbxMaxProb.setMaximum(1);
        self.spbxMaxProb.setMinimum(0);
        self.spbxMaxProb.setSingleStep(0.1);
        self.spbxMaxProb.setMaximumWidth(70);
        self.spbxMaxProb.setValue(0.7);
        self.btnGenerate = QtGui.QPushButton("Generate", self.mainWin);
        self.btnGenerate.setMaximumWidth(60);
        self.btnGenerate.connect(self.btnGenerate, QtCore.SIGNAL("clicked()"), self.onBtnGenerateClick);
        
        #Add to Layout
        self.gridLayout = QtGui.QGridLayout();
        self.gridLayout.setMargin(0);
        self.gridLayout.setSpacing(2);
        self.gridLayout.addWidget(self.lblMaxProb, 0,0, QtCore.Qt.AlignRight);
        self.gridLayout.addWidget(self.spbxMaxProb, 0,1);
        self.gridLayout.addWidget(self.btnGenerate, 1,0, 1,2, QtCore.Qt.AlignRight);
        self.qWidget = QtGui.QWidget(self.mainWin);
        self.qWidget.setLayout(self.gridLayout);
        self.vBoxMain = QtGui.QVBoxLayout();
        self.vBoxMain.addWidget(self.qWidget); 
        
                
